from pandas import read_csv, DataFrame
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import numpy as np
from sklearn.svm import SVR
from util import TIME_STEP, series_to_supervised, draw_data, UNITS_NUM

def no_attention():
    # 切换19年和20年数据只需要改下面两行
    multi_dataset = read_csv('./data/2019allday.csv', header=0, index_col=0)
    day_num = 26  # 数据包含的天数


    dataset = DataFrame()
    # 取in_card_flow(流出机场客流)、实际降落载客数 arr_ALDT_passenger、时段、天气作为参数，预测in_card_flow
    # dataset['in_flow'] = multi_dataset['in_flow']
    dataset['out_flow'] = multi_dataset['out_flow']
    # dataset['arr_ALDT_passenger'] = multi_dataset['arr_ALDT_passenger']
    dataset['arr_SIBT_passenger'] = multi_dataset['arr_SIBT_passenger']
    dataset['dep_ATOT_passenger'] = multi_dataset['dep_ATOT_passenger']
    dataset['dep_SOBT_passenger'] = multi_dataset['dep_SOBT_passenger']

    dataset['hour'] = multi_dataset['hour']
    dataset['weather'] = multi_dataset['weather']
    dataset['workday'] = multi_dataset['workday']

    dataset.fillna(0, inplace=True)

    PARAMETER_NUM = dataset.shape[1]  # 使用的数据种类数
    values = dataset.values
    # ensure all data is float
    values = values.astype('float32')

    # 构建监督学习问题
    reframed = series_to_supervised(values, TIME_STEP, 1)  # 6步预测下一步
    # 丢弃我们并不想预测的列
    reframed.drop(reframed.columns[[-1, -2, -3, -4, -5, -6]], axis=1, inplace=True)

    # 分割为训练集和测试集
    values = reframed.values
    n_train_time_slice = (day_num - 1) * 100
    train = values[:n_train_time_slice, :]
    test = values[n_train_time_slice:, :]
    # 分为输入输出
    train_X, train_y = train[:, :-1], train[:, -1]
    test_X, test_y = test[:, :-1], test[:, -1]

    # 设计网络
    model=SVR(kernel='rbf',C=1000)

    # 拟合神经网络模型
    model.fit(train_X, train_y)


    # 做出预测
    yhat = model.predict(test_X)
    # print(("预测值",yhat))
    inv_y=test_y
    inv_yhat=yhat



    mse = mean_squared_error(inv_y, inv_yhat)
    mae = mean_absolute_error(inv_y, inv_yhat)
    r2 = r2_score(inv_y, inv_yhat)
    # return mse,mae

    return mse,mae,r2,inv_yhat


if __name__ == '__main__':
    mse = 0
    mae = 0
    r2 = 0
    N = 10
    temp = np.zeros(94)
    for i in range(N):
        a, b, c, inv_yhat = no_attention()
        mse = mse + a
        mae = mae + b
        r2 = r2 + c
        temp = temp + np.array(inv_yhat)
    mse = mse / N
    mae = mae / N
    r2 = r2 / N
    temp = temp / N
    print("mse:%.3f, mae: %.3f, r2: %.3f" % (mse, mae, r2))
    np.save("./result/out/SVR", temp)



